package com.hyxt.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.hyxt.api.ChainLinkServer;
import com.hyxt.dao.RedisServiceDAO;
import com.hyxt.handler.ServerHandler;
import com.hyxt.provider.RedisService;
import com.hyxt.utils.Caches;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author songm
 * @version v1.0
 * @Description 接收主链路服务端809消息
 * @Date: Create in 15:29 2017/12/19
 * @Modifide By:
 **/
@Service("receive809ServerMsg")
public class Receive809ServerMsg implements IReceiveMsg {

    //809链路管理
    @Reference(version = "1.0.0")
    private ChainLinkServer chainLinkServer;

    /**
     * 监听809服务端消息
     * @param channelName 监听的通道名
     * @return
     */
    @Override
    public Boolean listen(final String channelName) {

        if (channelName != null) {
            //开启一个线程持续监听通道
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true){
                        // todo 调用链路管理监听接口
                        String message = chainLinkServer.receiveMsg(channelName);
                        ServerHandler handler = (ServerHandler) Caches.serverHandlers.get(channelName);
                        if (handler == null){
                            continue;
                        }
                        handler.setMainChannel(channelName);
                        handler.process(message);
                    }
                }
            }).start();
            return true;
        }
        return false;
    }

}
